//This file created by Barry Hunter www.nearby.org.uk (c) Oct 2010


var addressMarker;

//the ajax request object
var request;

//array of markers so we can keep track of which to remove
var marks = new Array();
var shown =0;

//shortcut to the message div
var m;

//is the a fetch in progress?
var running = false;

//these are for zoomin optimization (if prev zoom had all markers then no need to load them again for zooming in)
var prevZoom = -1;
var shownall = false;
var sentBounds = '';



function createMarker(point,id,html,realname,icon,link) {


	var options = {
		position: point,
		draggable: false
	};
	options.title = html;
	if (icon.length > 0) {
		if (shown < 25) {
			options.icon = new google.maps.MarkerImage(icon,new google.maps.Size(32, 32),new google.maps.Point(0, 0),new google.maps.Point(16,16),new google.maps.Size(32, 32));
		} else {
			options.icon = new google.maps.MarkerImage("http://s0.geograph.org.uk/discuss/img/topic_updated.gif",new google.maps.Size(10, 10),new google.maps.Point(0, 0),new google.maps.Point(5,5),new google.maps.Size(10, 10));
		}
		html = html+"<br/><img src=\""+icon+"\" align=\"left\" />";
	}
	var marker = new google.maps.Marker(options);
	
	if (link.length >0) {
		html = '<a href="'+link+'">'+html+'</a>';
	}
	if (realname.length >0) {
		html = html+'<br/> by '+realname;
	}
	
	var infowindow = new google.maps.InfoWindow({
	    content: html
	});
	
	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map,marker);
	});
	
	return marker;
}

function refresh_map() {
	if (running) {
		if (request)
			request.abort();
	}
	running = false;
	request = null;
	//todo map.clearOverlays();
	marks = new Array();
	update_map();
}

function update_map() {
	if (running) {
		if (request)
			request.abort();
		running = false;
	}
	if (shownall == false || map.getZoom() <= prevZoom) {
		var bounds = map.getBounds();

		var zoom = map.getZoom();
		
		sentBounds = bounds.toString()
		
		m = document.getElementById("message");
		m.innerHTML = "Requesting Results...";
		
		request = $.getJSON("/images.ajax.php?view=json&bounds="+sentBounds,function (data) {
			
			m.innerHTML = "Parsing Results..";
			//flag all current markers as old
			for (i in marks) 
				if (marks[i] != null) {
					marks[i].old = true;
					shown=shown-1;
				}
			
			m.innerHTML = "Adding "+(data.length)+" Markers...";
			for (var i = 0; i < data.markers.length; i++) {
				id = data.markers[i].gridimage_id;

				if (marks[id] && marks[id] != null) {
					//we have this one so lets flag it as valid
					marks[id].old = false;
				} else {
					var point = new google.maps.LatLng(parseFloat(data.markers[i].wgs84_lat), parseFloat(data.markers[i].wgs84_long));

					//add any extra fields to this line
					marks[id] = createMarker(point,id,data.markers[i].title,data.markers[i].realname,data.markers[i].thumbnail,"http://www.geograph.org.uk/photo/"+id);

					marks[id].setMap(map);
					
				}
				shown=shown+1;
			}

			m.innerHTML = "Removing Old Markers...";
			for (i in marks) 
				if (marks[i] != null) 
					if (marks[i].old == true) {
						marks[i].setMap(null);
						marks[i] = null; //marks.splice(i,1);
						
					}

			
			//print out a message
			var count = data.count;
			
			if (count && count.length > 0) {
				if (data.markers.length == count) {
					m.innerHTML = "Finished, showing "+data.markers.length+" markers.";
					shownall = true;
				} else {
					m.innerHTML = "Finished, showing random "+data.markers.length+" of "+count+" markers, Zoom-in to see more!"+shown;
					shownall = false;
				}
			} else {
				m.innerHTML = "Finished, showing "+data.markers.length+" of unknown markers."+shown;
				shownall = true;
			}
			running = false;
		});

	}
	prevZoom = map.getZoom();
}